package com.yanqu.road.dao.impl.activity.flowerstudy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.flowerstudy.data.cross.CrossFlowerStudyExamRoomData;
import com.yanqu.road.entity.activity.flowerstudy.data.cross.CrossFlowerStudyUserBattleData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FlowerStudyCrossExamRoomDataDaoImpl extends TempDataObjectDao<CrossFlowerStudyExamRoomData> {

    public static String getCrossFlowerStudyExamRoomDataMapSql = "select * from `t_c_flower_study_activity_room` where `activityId`=? and `roomState`=?";
    public Map<String,CrossFlowerStudyExamRoomData> getCrossFlowerStudyExamRoomDataMap(int activityId,int roomState) {
        Map<String,CrossFlowerStudyExamRoomData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFlowerStudyExamRoomDataMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, roomState);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossFlowerStudyExamRoomData parse = parse(rs);
                map.put(parse.getRoomId(),parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getCrossFlowerStudyExamRoomDataSql = "select * from `t_c_flower_study_activity_room` where `activityId`=? and `roomId`=?";
    public CrossFlowerStudyExamRoomData getCrossFlowerStudyExamRoomData(int activityId,String roomId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFlowerStudyExamRoomDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setString(i++, roomId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateCrossFlowerStudyExamRoomDataSql = "update `t_c_flower_study_activity_room` set roomType=?,roomState=?,userBattleDataMap=?,questionList=?,nowQuestionIndex=?,matchEndTime=?,thisQuestionStartTime=?,endTime=? where activityId=? and roomId=?";
    @Override
    protected boolean updateData(CrossFlowerStudyExamRoomData crossFlowerStudyExamRoomData) {
        crossFlowerStudyExamRoomData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamRoomData.getRoomType()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamRoomData.getRoomState()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFlowerStudyExamRoomData.getUserBattleDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFlowerStudyExamRoomData.getQuestionList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamRoomData.getNowQuestionIndex()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyExamRoomData.getMatchEndTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyExamRoomData.getThisQuestionStartTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyExamRoomData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamRoomData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossFlowerStudyExamRoomData.getRoomId()));
        if (getDb().executeNoneQuery(updateCrossFlowerStudyExamRoomDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossFlowerStudyExamRoomDataSql = "insert into `t_c_flower_study_activity_room`(activityId,roomId,roomType,roomState,userBattleDataMap,questionList,nowQuestionIndex,matchEndTime,thisQuestionStartTime,endTime) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossFlowerStudyExamRoomData crossFlowerStudyExamRoomData) {
        crossFlowerStudyExamRoomData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamRoomData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossFlowerStudyExamRoomData.getRoomId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamRoomData.getRoomType()));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamRoomData.getRoomState()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFlowerStudyExamRoomData.getUserBattleDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossFlowerStudyExamRoomData.getQuestionList())));
        para.put(i++, new DbParameter(Types.INTEGER, crossFlowerStudyExamRoomData.getNowQuestionIndex()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyExamRoomData.getMatchEndTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyExamRoomData.getThisQuestionStartTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossFlowerStudyExamRoomData.getEndTime()));
        if (getDb().executeNoneQuery(insertCrossFlowerStudyExamRoomDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossFlowerStudyExamRoomData parseData(ResultSet rs) throws SQLException {
        CrossFlowerStudyExamRoomData crossFlowerStudyExamRoomData = new CrossFlowerStudyExamRoomData();
        crossFlowerStudyExamRoomData.setActivityId(rs.getInt("activityId"));
        crossFlowerStudyExamRoomData.setRoomId(rs.getString("roomId"));
        crossFlowerStudyExamRoomData.setRoomType(rs.getInt("roomType"));
        crossFlowerStudyExamRoomData.setRoomState(rs.getInt("roomState"));
        crossFlowerStudyExamRoomData.setUserBattleDataMap(JSON.parseObject(rs.getString("userBattleDataMap"), new TypeReference<ConcurrentHashMap<Long, CrossFlowerStudyUserBattleData>>() {}));
        crossFlowerStudyExamRoomData.setQuestionList(JSON.parseObject(rs.getString("questionList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        crossFlowerStudyExamRoomData.setNowQuestionIndex(rs.getInt("nowQuestionIndex"));
        crossFlowerStudyExamRoomData.setMatchEndTime(rs.getLong("matchEndTime"));
        crossFlowerStudyExamRoomData.setThisQuestionStartTime(rs.getLong("thisQuestionStartTime"));
        crossFlowerStudyExamRoomData.setEndTime(rs.getLong("endTime"));

        return crossFlowerStudyExamRoomData;
    }
}
